package com.iengine.services.api.command.music;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.iengine.services.api.utils.Constant;
import com.iengine.services.api.utils.OperationName;
import com.ingine.data.pojo.HReply;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class CommandMusic extends AbstractMusicPhoneApiCommand{
	
	public static final String FIELD_SINGERS = "Singers";
	public static final String FIELD_SONGS = "Songs";
	public static final String FIELD_ALBUM = "Album";
	public static final String FIELD_SONGTYPE = "SongType";
	public static final String FIELD_FAVORITELIST = "FavoriteList";
	public static final String FIELD_PAGE_NUMBER="pageNumber";
	public static final String FIELD_PAGE_SIZE="pageSize";

	/**
	 * ClassName[String]:CommandMusic  
	 * 	arg-list:
	 * 	Singers[String]:歌手[#歌手[#歌手]]
	 * 	Songs[String]:歌曲[#歌曲[#歌曲]]
	 *  Album[String]:专辑名[#专辑名[#专辑名]]
	 *  SongType[String]:歌曲类型[#歌曲类型[#歌曲类型]]
	 *  FavoriteList[boolean]:是否播放收藏列表
	 */
	@Override
	protected Map formatArgs(Map map, Map nlpParams) {

		String favoriteList = getValue(nlpParams.get(FIELD_FAVORITELIST), null);
		if (StringUtils.isNotBlank(favoriteList)) {
			map = this.doFavoriteList(map, favoriteList);
		} else {
			HReply reply = getRemoteData(map, nlpParams);
			map = this.getFormatResult(reply, map);
		}

		return map;
	}
	
	

	protected Map getFormatResult(HReply reply,Map map){
		Map<String,Object> resultMap =new HashMap<String,Object>();
		Map  listMap=new HashMap<String,Object>();
		listMap.put(Constant.OPERATION_NAME,OperationName.sequence.name());
		listMap.put(Constant.MUSIC_INFO, reply.getContent());
		resultMap.put(Constant.COMMAND_NAME, Constant.COMMAND_MEDIA_OPERATION);
		resultMap.put(Constant.DATA, listMap);
		map.put(Constant.COMMAND_DATA, resultMap);
		return map;
	}
	
	public String processQuery(Map map,Map nlpParams){
		Map temp = new HashMap<String, Object>();
		temp.putAll(nlpParams);
    	Object dataSize = map.get(Constant.FIELD_DATASIZE);
    	if(null != dataSize){
    		temp.putAll((Map)dataSize);
    	}
		String singers = getValue(nlpParams.get(FIELD_SINGERS),null);
		String songers = getValue(nlpParams.get(FIELD_SONGS),null);
		String album = getValue(nlpParams.get(FIELD_ALBUM),null);
		String songType = getValue(nlpParams.get(FIELD_SONGTYPE),null);
		String pageNumber = getValue(nlpParams.get(FIELD_PAGE_NUMBER),null);
		String pageSize = getValue(nlpParams.get(FIELD_PAGE_SIZE),null);
	    Map parameterMap=new HashMap();
	    if(StringUtils.isNoneBlank(pageNumber) && StringUtils.isNoneBlank(pageSize)){
	    	parameterMap.put(CommandMusic.FIELD_PAGE_NUMBER, pageNumber);
	    	parameterMap.put(CommandMusic.FIELD_PAGE_SIZE, pageSize);
	    }
		if(StringUtils.isNoneBlank(singers) && StringUtils.isNoneBlank(songers) && StringUtils.isNoneBlank(album) && StringUtils.isNotBlank(songType)){
			parameterMap.put(CommandMusic.FIELD_SINGERS, singers);
			parameterMap.put(CommandMusic.FIELD_SONGS, songers);
			parameterMap.put(CommandMusic.FIELD_ALBUM, album);
			parameterMap.put(CommandMusic.FIELD_SONGTYPE, songType);
		}else if(StringUtils.isNoneBlank(singers) && StringUtils.isNoneBlank(songers) && StringUtils.isNoneBlank(album)){
			parameterMap.put(CommandMusic.FIELD_SINGERS, singers);
			parameterMap.put(CommandMusic.FIELD_SONGS, songers);
			parameterMap.put(CommandMusic.FIELD_ALBUM, album);
		}else if(StringUtils.isNoneBlank(singers) && StringUtils.isNoneBlank(songers)){
			parameterMap.put(CommandMusic.FIELD_SINGERS, singers);
			parameterMap.put(CommandMusic.FIELD_SONGS, songers);
		}else if(StringUtils.isNoneBlank(singers)){
			parameterMap.put(CommandMusic.FIELD_SONGS, singers);
		}else{

		}
		parameterMap.put(Constant.DATA_TYPE_FIELD, Constant.MUSIC_FIELD);
		parameterMap.put(Constant.PARAMS_DATA_FIELD, temp);
		return new Gson().toJson(parameterMap);
	}
	
	private Map doFavoriteList(Map map,String favoriteList) {
		Map<String,Object> subMap=new HashMap<String,Object>();
		if(StringUtils.equals(favoriteList, OperationName.mediaFavoriteList.toString())){
			subMap.put(Constant.OPERATION_NAME, OperationName.mediaFavoriteList.name());
		}
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put(Constant.COMMAND_NAME, Constant.COMMAND_MEDIA_OPERATION);
		resultMap.put(Constant.DATA, subMap);
		map.put(Constant.COMMAND_DATA, resultMap);
		return map;
	}
	
	@Override
	public String getTTs() {
		return "正在为您加载";
	}
	
}
